1054C - Candies Distribution - CodeForces Solution


constructive algorithms implementation *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;
//               to make it ordered multiset-->   less_equal

#define fast             ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define L                cout<<'\n';
#define ll               long long
#define int           ll int
#define rep(i,a,b)       for (int i=a; i<b; ++i)
#define rev(i,a,b)       for (int i=a; i>b; --i)
#define setpr(x)         cout<<setprecision(x)<<fixed   //cerr

#define vc               vector
#define pb               push_back
#define p                push
#define all(v)           v.begin(), v.end()
#define rall(v)           v.rbegin(), v.rend()
#define sz(s)             s.size()
#define F                 first
#define S                 second

#define max(a, b)         (a < b ? b : a)
#define min(a, b)         ((a > b) ? b : a)

int const MOD =          1e9 + 7;
long long mod(long long x)  {return ((x % MOD + MOD) % MOD);}
long long add(long long a, long long b)  {return mod(mod(a) + mod(b));}
long long mulitiply(long long a, long long b)  {return mod(mod(a) * mod(b));}

int gcd(int a, int b)     { if (a == 0) return b; return gcd(b % a, a);}

int qrt(int x)          { int lo = 1, hi = min(2000000ll, x); while (hi - lo > 1) {int mid = (lo + hi) / 2; if (mid * mid * mid < x) {lo = mid;} else hi = mid;} if (hi * hi * hi <= x) return hi; else return lo;}

void Sieve_Erthenes(int n) {
    vc<bool> prm(n + 1, true); prm[0] = prm[1] = false;
    for (int i = 2; i * i <= n; i++) {if (prm[i]) {for (int j = i * i; j <= n; j += i) prm[j] = false;}}
    for (int p = 2; p <= n; p++) if (prm[p])  cout << p << " ";
}

int binExpo(int a, int b, int m) {
    a %= m; int res = 1;
    while (b > 0) {
        if (b & 1) res = (res * (a) * 1ll) % m; a = (a * 1ll * a) % m;  b >>= 1;
    }
    return res;
}



#define PIE              3.14159265358979323846
#define INF               1e18
int dx[4] =           { -1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int XX[] =             { -1, -1, -1, 0, 0, 1, 1, 1 }, YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

#define ms(a,b)           memset(a, b, sizeof(a))
// #define fill(arr,n+1)   fill(arr, arr+n+1, INT_MAX); in every element of arr
#define bit_no            __builtin_popcount

#define mxpq               priority_queue<int>
#define mnpq               priority_queue<int, vector<int>, greater<int> >
typedef vector<int>                vi;
typedef vector<vi>                 vvi;
typedef pair<int, int>             ii;
typedef tuple<int, int, int>       iii;
typedef vector<ii>                 vii;
typedef vector<iii>                viii;

void solve() {

    ll n;
    cin >> n;
    vector<ll>left(n), right(n), a(n);
    for (int i = 0; i < n; i++)
    {
        cin >> left[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> right[i];
        a[i] = (n - left[i] - right[i]);
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < i; j++)
        {
            left[i] -= (a[j] > a[i]);
        }
        for (int j = i + 1; j < n; j++)
        {
            right[i] -= (a[j] > a[i]);
        }
        if (left[i] || right[i]) {cout << "NO" << endl; return;}
    }
    cout << "YES" << endl;
    for (auto &e : a) {cout << e << " ";}
}


int32_t main () {
    fast
    int test_case = 1; // cin >> test_case;

    while (test_case--) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations